package com.xiaoyu.lanling.feature.voicematch.data

import `in`.srain.cube.request.JsonData
import `in`.srain.cube.request.RequestDefaultHandler
import com.xiaoyu.base.event.NoReceiverJsonEvent
import com.xiaoyu.base.net.RequestCreator
import com.xiaoyu.lanling.common.config.HttpConfig
import com.xiaoyu.lanling.event.voicematch.*
import com.xiaoyu.lanling.feature.voicematch.datamodel.EvaluateEnum

class VoiceMatchData {

    companion object {
        const val MATCH_STATUS_INIT = 0
        const val MATCH_STATUS_WAITING = 1
        const val MATCH_STATUS_MATCHED = 2
        const val MATCH_STATUS_EXIT = 3

        fun applyMatch(requestTag: Any) {
            RequestCreator.create<VoiceMatchApplyMatchEvent>(requestTag, VoiceMatchApplyMatchEvent::class.java)
                    .setPostEventWhenFail(true)
                    .setRequestUrl(HttpConfig.VOICE_MATCH_APPLY_MATCH)
                    .enqueue()
        }

        fun exitMatch(requestTag: Any) {
            RequestCreator.create<VoiceMatchExitMatchEvent>(requestTag, VoiceMatchExitMatchEvent::class.java)
                    .setRequestUrl(HttpConfig.VOICE_MATCH_EXIT_MATCH)
                    .setPostEventWhenFail(true)
                    .enqueue()
        }

        fun establishConnection(fromUid: String) {
            RequestCreator.create<NoReceiverJsonEvent>(Any(), NoReceiverJsonEvent::class.java)
                    .setRequestUrl(HttpConfig.VOICE_MATCH_ESTABLISH_CONNECTION)
                    .addPostData("fromUid", fromUid)
                    .enqueue()
        }

        fun hangup(fuid: String?, type: String?) {
            RequestCreator.create<NoReceiverJsonEvent>(Any(), NoReceiverJsonEvent::class.java)
                    .setRequestUrl(HttpConfig.VOICE_MATCH_HANGUP)
                    .addPostData("anotherUid", fuid)
                    .addPostData("typeForLog", type)
                    .enqueue()
        }

        fun isInQueue(requestTag: Any) {
            RequestCreator.create<VoiceMatchIsInQueueEvent>(requestTag, VoiceMatchIsInQueueEvent::class.java)
                    .setRequestUrl(HttpConfig.VOICE_MATCH_IN_QUEUE)
                    .enqueue()
        }

        fun checkEntryStatus(requestTag: Any, from: String) {
            RequestCreator.create<VoiceMatchCheckEntryStatusEvent>(VoiceMatchCheckEntryStatusEvent::class.java)
                    .setRequestUrl(HttpConfig.VOICE_MATCH_CHECK_ENTRY_STATUS)
                    .setRequestHandler(object : RequestDefaultHandler<VoiceMatchCheckEntryStatusEvent, JsonData>() {
                        override fun processOriginData(originData: JsonData): VoiceMatchCheckEntryStatusEvent {
                            val jsonData = originData.optJson("data")
                            return VoiceMatchCheckEntryStatusEvent(requestTag, jsonData, from)
                        }

                        override fun onRequestFinish(event: VoiceMatchCheckEntryStatusEvent?) {
                            event?.post()
                        }
                    })
                    .enqueue()
        }

        fun currentRank(requestTag: Any) {
            RequestCreator.create<VoiceMatchCurrentRankEvent>(requestTag, VoiceMatchCurrentRankEvent::class.java)
                    .setRequestUrl(HttpConfig.VOICE_MATCH_CURRENT_RANK)
                    .enqueue()
        }

        fun matchEvaluate(requestTag: Any, uid: String, evaluate: EvaluateEnum) {
            RequestCreator.create<NoReceiverJsonEvent>(requestTag, NoReceiverJsonEvent::class.java)
                    .setRequestUrl(HttpConfig.VOICE_MATCH_MATCH_EVALUATE)
                    .addPostData("uid", uid)
                    .addPostData("evaluate", evaluate.type)
                    .enqueue()
        }
    }
}